//
//  hdfs_client.h
//  DataPlatform
//
//  Created by Liu Dafan on 12-2-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#ifndef HDFS_CLIENT_H
#define HDFS_CLIENT_H

#include <arpa/inet.h>
#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>

#include <ThriftHadoopFileSystem.h>
#include "base.h"

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
//using namespace apache::hadoop;

using namespace boost;

class HdfsOp;

class HdfsFile
{
    friend class HdfsOp;
protected:
    HdfsFile(ThriftHandle h)
    {
        _handle = h;
    }
private:
    ThriftHandle _handle;
};

typedef HdfsFile* HdfsHandle;

enum FILE_MODE
{
    FILE_MODE_CREATE = 0,
    FILE_MODE_READ,
    FILE_MODE_APPEND
};

class HdfsOp
{
public:
	HdfsOp(char *_sv_ip, unsigned short _sv_port);
	~HdfsOp();
    
    int init();
    void uninit();
    
    HdfsHandle open(const std::string& filename, FILE_MODE mode);
    
    int writebytes(HdfsHandle handle, const int8_t* data, size_t size);
    int writebytes(HdfsHandle handle, const std::vector<int8_t>& vec_data);
    
    int readbytes(HdfsHandle handle, int offset, long length, int8_t*& data, size_t& size);
    int readbytes(HdfsHandle handle, int offset, long length, std::vector<int8_t>& vec_data);
    
    int getlength(const std::string& filename, uint64_t& length);
    
    int listfiles(const std::string& pathname, std::vector<FileStatus>& files);
    
    bool isExist(const std::string& filename);
    
    void close(HdfsHandle handle);

public:
	char _sv_ip[INET_ADDRSTRLEN];
	unsigned short _sv_port;
    bool _isInit;
	ThriftHadoopFileSystemClient *_hclient;
    shared_ptr<TTransport> _socket;
    shared_ptr<TTransport> _transport;
    shared_ptr<TProtocol> _protocol;
	int _errnum;
};

#endif
